import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author: wh(1835734390 @ qq.com)
 * @date: 2023/4/24 9:04
 * @description:
 * @version:
 */
public class RunnableTask {
    public static void main(String[] args) {
        AtomicInteger taskNum = new AtomicInteger();
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        long start1 = System.currentTimeMillis();
        for (int i = 0; i < 5; i++) {
            Runnable runnableTask = () -> {
                long start = System.currentTimeMillis();
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                long end = System.currentTimeMillis();
                long time = end - start;
                System.out.println(Thread.currentThread().getName() + "该线程所消耗时间" + time);
                taskNum.incrementAndGet();
                System.out.println(taskNum.get());
            };
            executorService.submit(runnableTask);
        }
        while (true) {
            if (taskNum.get() == 5) {
                System.out.println("当前taskNum为" + taskNum.get());
                break;
            } else {
                System.out.println("当前taskNum为" + taskNum.get());
            }
        }
        long end = System.currentTimeMillis();
        long totalTime = end - start1;
        System.out.println("程序总耗时为" + totalTime);
        executorService.shutdown();
    }


}
